﻿//-----------------------------------------------------------------------
// <copyright file="ChoosePositionFromMapPage.xaml.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BookCaching.Settings;
using Microsoft.Phone.Controls;
using BookCaching.Localization;

namespace BookCaching.Pages
{
    /// <summary>
    /// Page displays a map and it gives the user the ability to choose 
    /// a position from the map and get it's coordinates.
    /// </summary>
    public partial class ChoosePositionFromMapPage : PhoneApplicationPage
    {
        private GeoCoordinate selectedPosition;
        private int bookID;

        public ChoosePositionFromMapPage()
        {
            this.InitializeComponent();
            selectedPosition = new GeoCoordinate();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            selectedPositionPushpin.Visibility = System.Windows.Visibility.Collapsed;
            map.Center = CurrentSettings.Instance.CurrentPosition;
            map.ZoomLevel = DefaultSettings.Instance.MapZoomLevel;

            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.ContainsKey("BookID"))
            {
                bookID = Convert.ToInt32(NavigationContext.QueryString["BookID"]);
            }
        }

        private void client_ReturnBookCompleted(object sender, Knihotoc_WCFService.ReturnBookCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                return;
            }
            if (e.Result != null)
            {
                MessageBox.Show(e.Result);
            }
            else
            {
                MessageBox.Show(LocalizedStrings.Get("BookReturned"));
            }
            NavigationService.Navigate(new Uri("/Pages/MainPage.xaml", UriKind.Relative));
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            float lat = (float)selectedPosition.Latitude;
            float lon = (float)selectedPosition.Longitude;
            string description = "";
            int? place = null;
            Knihotoc_WCFService.Knihotoc_WP7ServiceClient client = new Knihotoc_WCFService.Knihotoc_WP7ServiceClient();

            try
            {
                client.ReturnBookCompleted += client_ReturnBookCompleted;
                client.ReturnBookAsync(bookID, place, lat, lon, description);
            }
            catch
            {
                return;
            }
        }

        private void map_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Point tapPoint = e.GetPosition(this.map);
            selectedPosition = map.ViewportPointToLocation(tapPoint);

            positionLatitudeText.Text = selectedPosition.Latitude.ToString();
            positionLongitudeText.Text = selectedPosition.Longitude.ToString();

            selectedPositionPushpin.Visibility = System.Windows.Visibility.Visible;
            ////in.Location = selectedPosition;
            map.Center = selectedPosition;
            if (map.ZoomLevel < DefaultSettings.Instance.MapZoomLevel)
            {
                map.ZoomLevel = DefaultSettings.Instance.MapZoomLevel;
            }
        }
    }
}